from .fs_services import file_exists

import ujson
try:
    from shared_types import *
except ImportError:
    pass
UDP_DISCOVERY_PORT = 10130


class Configs:
    def __init__(self) -> None:
        self.file = "config.json"
        if not file_exists(self.file):
            self.config: "SystemBasicConfig" = {
                "ssid": "",
                "password": "",
                "role": 'locomotive-control',
                "name": "Untitled-Controller"
            }
            with open(self.file, 'w') as f:
                ujson.dump(self.config, f)
        with open(self.file, 'r') as f:
            self.config = ujson.loads(f.read())

    def update(self, items: dict):
        for k, v in items.items():
            self.config[k] = v
        with open(self.file, 'w') as f:
            ujson.dump(self.config, f)

configs = Configs()